package com.lswms.modules.lswms.service.impl.recipient;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.runtime.typehandling.BigDecimalMath;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.Constant.InventoryConstant;
import com.lswms.modules.lswms.Constant.LocationConstant;
import com.lswms.modules.lswms.Constant.RecipientConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.Constant.TradeConstant;
import com.lswms.modules.lswms.dao.base.LocationDao;
import com.lswms.modules.lswms.dao.recipient.RecipientsDao;
import com.lswms.modules.lswms.dao.recipient.RecipientsbackDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.stock.LotDao;
import com.lswms.modules.lswms.dao.stock.TradeDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecodeDao;
import com.lswms.modules.lswms.dao.uniquecode.UniquecoderecordDao;
import com.lswms.modules.lswms.dto.recipient.RecipientsbackDTO;
import com.lswms.modules.lswms.entity.base.LocationEntity;
import com.lswms.modules.lswms.entity.recipient.RecipientsEntity;
import com.lswms.modules.lswms.entity.recipient.RecipientsbackEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.stock.LotEntity;
import com.lswms.modules.lswms.entity.stock.TradeEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecodeEntity;
import com.lswms.modules.lswms.entity.uniquecode.UniquecoderecordEntity;
import com.lswms.modules.lswms.service.base.SequenceService;
import com.lswms.modules.lswms.service.recipient.RecipientsbackService;
import java.text.SimpleDateFormat;

/**
 * 归还记录
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-07-21
 */
@Service
public class RecipientsbackServiceImpl extends
		CrudServiceImpl<RecipientsbackDao, RecipientsbackEntity, RecipientsbackDTO> implements RecipientsbackService {

	@Autowired
	InventoryDao inventoryDao;

	@Autowired
	LocationDao locationDao;

	@Autowired
	LotDao lotDao;

	@Autowired
	TradeDao tradeDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	RecipientsDao recipientsDao;

	@Autowired
	SequenceService sequenceService;

	@Autowired
	private UniquecodeDao uniquecodeDao;

	@Autowired
	private UniquecoderecordDao uniquecoderecordDao;

	@Override
	public QueryWrapper<RecipientsbackEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String cardid = (String) params.get("cardid");
		String recipient = (String) params.get("recipient");
		String sku = (String) params.get("sku");
		String descr = (String) params.get("descr");
		String recipientkey = (String) params.get("recipientkey");
		String lot7 = (String) params.get("lot7");
		String recipientid = (String) params.get("recipientid");
		String lot10 = (String) params.get("lot10");

		String starttime = (String)params.get("starttime");
		String endtime = (String)params.get("endtime");


		QueryWrapper<RecipientsbackEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "lswms_Recipientsback.id", id);
		wrapper.eq(StringUtils.isNotBlank(cardid), "lswms_Recipients.cardid", cardid);
		wrapper.eq(StringUtils.isNotBlank(recipient), "lswms_Recipients.recipient", recipient);
		wrapper.eq(StringUtils.isNotBlank(recipientid), "lswms_Recipients.recipientid", recipientid);
		wrapper.eq(StringUtils.isNotBlank(sku), "lswms_sku.sku", sku);
		wrapper.like(StringUtils.isNotBlank(descr), "lswms_sku.descr", descr);

		wrapper.eq(StringUtils.isNotBlank(recipientkey), "lswms_Recipientsback.recipientkey", recipientkey);
		wrapper.eq(StringUtils.isNotBlank(lot7), "lswms_Recipientsback.lot7", lot7);
		wrapper.eq(StringUtils.isNotBlank(lot10), "lswms_Recipientsback.lot10", lot10);

		if (starttime != null && endtime != null){
			/*
			* 填了起始&&终止时间才能进行时间判断；两个时间参数确定哪个先，再进行QueryWrapper类的ge和le
			* */
			SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
			try {
				if (formatter.parse(starttime).compareTo(formatter.parse(endtime)) <= 0 ){
					starttime = starttime;
					endtime = endtime;
				}else if (formatter.parse(starttime).compareTo(formatter.parse(endtime)) > 0 ){
					endtime = starttime;
					starttime = endtime;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}else{
			starttime = null;
			endtime = null;
		}
		wrapper.ge(StringUtils.isNotBlank(starttime),"lswms_Recipientsback.addtime",starttime);//大于
		wrapper.le(StringUtils.isNotBlank(endtime),"lswms_Recipientsback.addtime",endtime);//小于



		wrapper.orderByDesc("lswms_Recipientsback.Recipientsbackkey");
		return wrapper;
	}

//	private String UDtime(String time) {
//		time = time.substring(0,10);
//		try{
//			//类型转换
//			SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
//			Date date = formatter.parse(time);
//
//			Calendar   calendar = new GregorianCalendar();
//			calendar.setTime(date);
//			calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
//			date=calendar.getTime(); //这个时间就是日期往后推一天的结果
//			time = formatter.format(date);
//			return time;
//		}catch (Exception e){
//			return null;
//		}
//	}

	@Override
	public PageData<RecipientsbackEntity> pageBySql(Map<String, Object> params) {
		IPage<RecipientsbackEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, RecipientsbackEntity.class);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String valideAndSave(RecipientsbackEntity dto) {
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
		// 校验数据
		BigDecimal backQty = dto.getBackqty();
		if (backQty == null || backQty.compareTo(BigDecimal.ZERO) <= 0) {
			return "数量必须为正数";
		}


	Date lot1Date = null;
	Date lot2Date = null;
	try {
		//String a = UDtime(dto.getSctime());

		if(dto.getSctime()!=null) {
			lot1Date = formatter.parse(dto.getSctime());
		}
		if(dto.getYxtime()!=null) {
			lot2Date = formatter.parse(dto.getYxtime());
		}
	} catch (ParseException e) {
		e.printStackTrace();
	}

		if (lot1Date != null) {
			if (lot1Date.compareTo(new Date()) > 0) {
				return "生产日期不能在今天日期之后";
			}

			if (lot2Date != null) {
				if (lot1Date.compareTo(lot2Date) >= 0) {
					return "生产日期不能与有效期相等或之后";
				}
			}
		}

		// 校验目的库位
		String toloc = dto.getTolocation();

		String toTray = dto.getTotray();
		if (toTray == null || toTray.trim().length() == 0) {
			return "箱号不能为空，请填写箱号";
		}
		toTray = toTray.trim();

		String toBox = dto.getTobox();
		if (toBox == null || toBox.trim().length() == 0) {
			return "格号不能为空，请填写格号";
		}
		toTray = toTray.trim();
		toBox = toBox.trim();

		// 使用的托盘是否在其他库位存在库存，若存在库存，则不允许收货
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("tray", toTray);
		inventoryQuery.ne("location", toloc);
		inventoryQuery.gt("qty", 0);

		Integer inventoryCount = inventoryDao.selectCount(inventoryQuery);
		if (inventoryCount > 0) {
			return "箱已在其他库位被使用,请更换箱号";
		}
		// 校验托盘是否有未完成的任务，有任务不允许使用
		QueryWrapper<TaskEntity> taskQuery = new QueryWrapper<TaskEntity>();
		// taskQuery.eq("tray", toTray);
		taskQuery.ne("status", TaskConstant.TASK_9);
		taskQuery.and(
				wrapper -> wrapper.eq("fromtray", dto.getTotray().trim()).or().eq("totray", dto.getTotray().trim()));

		Integer count = taskDao.selectCount(taskQuery);
		if (count > 0) {
			return "箱已在其他库位被使用,请更换箱号";
		}

		// 获取目的库位信息
		QueryWrapper<LocationEntity> locationWrapper = new QueryWrapper<>();
		locationWrapper.eq("location", toloc);
		LocationEntity locationEntity = locationDao.selectOne(locationWrapper);
		if (locationEntity == null || locationEntity.getLocation() == null) {
			return String.format(MEGConstant.LOC_NOT_EXISTS, toloc);
		}

		if (locationEntity.getLocStatus().equals(LocationConstant.LOC_STATUS_DAMAGE)) {
			return MEGConstant.LOC_STATUS_DAMAGE_ERROR;
		}

		if (locationEntity.getLocCategory().equals("STEREO")) {
			return "不能直接收货到立体库";
		}

		// 校验数量
		// 查询借用的数量
		QueryWrapper<RecipientsEntity> queryReciptient = new QueryWrapper<RecipientsEntity>();
		queryReciptient.eq("recipientkey", dto.getRecipientkey());

		RecipientsEntity recipientsEntity = recipientsDao.selectOne(queryReciptient);
		BigDecimal qty = recipientsEntity.getQty();

		// 查询已经归还的数据
		QueryWrapper<RecipientsbackEntity> queryRecipientsback = new QueryWrapper<RecipientsbackEntity>();
		queryRecipientsback.eq("recipientkey", dto.getRecipientkey());
		List<RecipientsbackEntity> recipientsbackEntityList = baseDao.selectList(queryRecipientsback);
		BigDecimal hasBackQty = BigDecimal.ZERO;
		for (RecipientsbackEntity recipientsbackEntity : recipientsbackEntityList) {
			hasBackQty = hasBackQty.add(recipientsbackEntity.getBackqty());
		}

		if (qty.compareTo(hasBackQty.add(backQty)) < 0) {
			return "归还后的数量超过领用数量，请重新填写数量";
		}

		// 将字段为null改为''
		dto.setLot3(dto.getLot3() == null ? StringUtils.EMPTY : dto.getLot3().trim());
		dto.setLot4(dto.getLot4() == null ? StringUtils.EMPTY : dto.getLot4().trim());
		dto.setLot5(dto.getLot5() == null ? StringUtils.EMPTY : dto.getLot5().trim());
		dto.setLot6(dto.getLot6() == null ? StringUtils.EMPTY : dto.getLot6().trim());
		dto.setLot7(dto.getLot7() == null ? StringUtils.EMPTY : dto.getLot7().trim());
		dto.setLot8(dto.getLot8() == null ? StringUtils.EMPTY : dto.getLot8().trim());
		dto.setLot9(dto.getLot9() == null ? StringUtils.EMPTY : dto.getLot9().trim());
		dto.setLot10(dto.getLot10() == null ? StringUtils.EMPTY : dto.getLot10().trim());
		dto.setLot11(dto.getLot11() == null ? StringUtils.EMPTY : dto.getLot11().trim());
		dto.setLot12(dto.getLot12() == null ? StringUtils.EMPTY : dto.getLot12().trim());
		dto.setLot13(dto.getLot13() == null ? StringUtils.EMPTY : dto.getLot13().trim());
		dto.setLot14(dto.getLot14() == null ? StringUtils.EMPTY : dto.getLot14().trim());
		dto.setLot15(dto.getLot15() == null ? StringUtils.EMPTY : dto.getLot15().trim());

		String lot10 = dto.getLot10();

		if (!lot10.equals("1")) {
			// 校验收货库位混放 0 可以混放，1 不允许混放
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("location", toloc);
			param.put("sku", dto.getSku());
			param.put("lot1", dto.getLot1());
			param.put("lot2", dto.getLot2());
			param.put("lot3", dto.getLot3());
			param.put("lot4", dto.getLot4());
			param.put("lot5", dto.getLot5());
			param.put("lot6", dto.getLot6());
			param.put("lot7", dto.getLot7());
			param.put("lot8", dto.getLot8());
			param.put("lot9", dto.getLot9());
			param.put("lot10", dto.getLot10());
			param.put("lot11", dto.getLot11());
			param.put("lot12", dto.getLot12());
			param.put("lot13", dto.getLot13());
			param.put("lot14", dto.getLot14());
			param.put("lot15", dto.getLot15());
			int ismixedCount = inventoryDao.canbeismixed(param);
			if (ismixedCount > 0) {
				return String.format(MEGConstant.LOC_NOT_MIXED, toloc);
			}

			// 查询是否存在相同批属性的lot记录
			// 存在：使用已有的lot号
			// 不存在：产生新的lot记录
			QueryWrapper<LotEntity> lotWrapper = new QueryWrapper<>();
			lotWrapper.eq("sku", dto.getSku());
//			lotWrapper.eq("lot1", dto.getLot1());
//			lotWrapper.eq("lot2", dto.getLot2());
			lotWrapper.eq("lot3", dto.getLot3());
			lotWrapper.eq("lot4", dto.getLot4());
			lotWrapper.eq("lot5", dto.getLot5());
			lotWrapper.eq("lot6", dto.getLot6());
			lotWrapper.eq("lot7", dto.getLot7());
			lotWrapper.eq("lot8", dto.getLot8());
			lotWrapper.eq("lot9", dto.getLot9());
			lotWrapper.eq("lot10", dto.getLot10());
			lotWrapper.eq("lot11", dto.getLot11());
			lotWrapper.eq("lot12", dto.getLot12());
			lotWrapper.eq("lot13", dto.getLot13());
			lotWrapper.eq("lot14", dto.getLot14());
			lotWrapper.eq("lot15", dto.getLot15());
			if (dto.getLot1() != null) {
				lotWrapper.and(wrapper -> wrapper.isNotNull("lot1").eq("lot1", dto.getLot1()));
			} else {
				lotWrapper.and(wrapper -> wrapper.isNull("lot1"));
			}
			if (dto.getLot2() != null) {
				lotWrapper.and(wrapper -> wrapper.isNotNull("lot2").eq("lot2", dto.getLot2()));
			} else {
				lotWrapper.and(wrapper -> wrapper.isNull("lot2"));
			}

			lotWrapper.orderByDesc("lot");
			List<LotEntity> LotEntityList = lotDao.selectList(lotWrapper);
			if (LotEntityList.size() > 0) {
				dto.setLot(LotEntityList.get(0).getLot());
			} else {
				LotEntity addLotEntity = new LotEntity();
				String lotNew = sequenceService.getSequence("lot");

				dto.setLot(lotNew);

				addLotEntity.setLot(lotNew);
				addLotEntity.setSku(dto.getSku());
				addLotEntity.setLot1(dto.getLot1());
				addLotEntity.setLot2(dto.getLot2());
				addLotEntity.setLot3(StringUtils.trimToEmpty(dto.getLot3()));
				addLotEntity.setLot4(StringUtils.trimToEmpty(dto.getLot4()));
				addLotEntity.setLot5(StringUtils.trimToEmpty(dto.getLot5()));
				addLotEntity.setLot6(StringUtils.trimToEmpty(dto.getLot6()));
				addLotEntity.setLot7(StringUtils.trimToEmpty(dto.getLot7()));
				addLotEntity.setLot8(StringUtils.trimToEmpty(dto.getLot8()));
				addLotEntity.setLot9(StringUtils.trimToEmpty(dto.getLot9()));
				addLotEntity.setLot10(StringUtils.trimToEmpty(dto.getLot10()));
				addLotEntity.setLot11(StringUtils.trimToEmpty(dto.getLot11()));
				addLotEntity.setLot12(StringUtils.trimToEmpty(dto.getLot12()));
				addLotEntity.setLot13(StringUtils.trimToEmpty(dto.getLot13()));
				addLotEntity.setLot14(StringUtils.trimToEmpty(dto.getLot14()));
				addLotEntity.setLot15(StringUtils.trimToEmpty(dto.getLot15()));
				lotDao.insert(addLotEntity);
			}

			// 相同的tray和box只存放同属性的品
			QueryWrapper<InventoryEntity> inventoryTrayAndBoxWrapper = new QueryWrapper<>();
			inventoryTrayAndBoxWrapper.eq("tray", toTray);
			inventoryTrayAndBoxWrapper.eq("box", toBox);
			inventoryTrayAndBoxWrapper.gt("qty", 0);
			inventoryTrayAndBoxWrapper.ne("lot", dto.getLot());
			int inventoryTrayAndBoxCount = inventoryDao.selectCount(inventoryTrayAndBoxWrapper);
			if (inventoryTrayAndBoxCount > 0) {
				return String.format(MEGConstant.TRAY_BOX_NOT_MIXED, toTray, toBox);
			}
		}
		// 设置行数

		dto.setTotray(toTray);
		dto.setTobox(toBox);
		dto.setLot3(StringUtils.trimToEmpty(dto.getLot3()));
		dto.setLot4(StringUtils.trimToEmpty(dto.getLot4()));
		dto.setLot5(StringUtils.trimToEmpty(dto.getLot5()));
		dto.setLot6(StringUtils.trimToEmpty(dto.getLot6()));
		dto.setLot7(StringUtils.trimToEmpty(dto.getLot7()));
		dto.setLot8(StringUtils.trimToEmpty(dto.getLot8()));
		dto.setLot9(StringUtils.trimToEmpty(dto.getLot9()));
		dto.setLot10(StringUtils.trimToEmpty(dto.getLot10()));
		dto.setLot11(StringUtils.trimToEmpty(dto.getLot11()));
		dto.setLot12(StringUtils.trimToEmpty(dto.getLot12()));
		dto.setLot13(StringUtils.trimToEmpty(dto.getLot13()));
		dto.setLot14(StringUtils.trimToEmpty(dto.getLot14()));
		dto.setLot15(StringUtils.trimToEmpty(dto.getLot15()));
		dto.setId(null);

		baseDao.insert(dto);
		if (!lot10.equals("1")) {
			String isuniquecode = dto.getIsuniquecode();
			// 效验数据
			if (StringUtils.isNotBlank(isuniquecode) && isuniquecode.equals("1")) {
				String uniquecodeBuild = dto.getUniquecodes();
				if (StringUtils.isBlank(uniquecodeBuild)) {
					return "请输入唯一码";
				}
				String[] uniquecodeBuilds = uniquecodeBuild.split("\n");

				List<String> uniquecodes = new LinkedList<>();
				for (String uniquecode : uniquecodeBuilds) {
					uniquecodes.add(uniquecode);
				}
				long count1 = uniquecodes.stream().distinct().count();
				if (uniquecodes.size() != count1) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "请不要输入重复唯一码";
				}
				BigDecimal backqty = dto.getBackqty();
				Number number = BigDecimalMath.abs(backqty);

				int i = number.intValue();
				if (i != uniquecodes.size()) {
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return "唯一码的数量不等于归还的数量";
				}
				for (String uniquecode : uniquecodes) {
					if (uniquecodeDao.checkUniquecode(uniquecode) > 0) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						return "此唯一码已存在:" + uniquecode;

					}
				}

				for (String uniquecode : uniquecodes) {
					// 增加唯一码记录
					UniquecodeEntity uniquecodeEntity = new UniquecodeEntity();
					uniquecodeEntity.setSku(dto.getSku());
					// uniquecodeEntity.setReceiptkey(dto.getRecipientsbackkey());
					uniquecodeEntity.setReceipttime(new Date());
					uniquecodeEntity.setTray(dto.getTotray());
					uniquecodeEntity.setBox(dto.getTobox());
					uniquecodeEntity.setLot(dto.getLot());
					uniquecodeEntity.setUniquecode(uniquecode);
					uniquecodeDao.insert(uniquecodeEntity);

					// 增加唯一码历史记录
					UniquecoderecordEntity uniquecoderecordEntity = new UniquecoderecordEntity();
					uniquecoderecordEntity.setUniquecode(uniquecode);
					uniquecoderecordEntity.setFromtray(StringUtils.EMPTY);
					uniquecoderecordEntity.setFrombox(StringUtils.EMPTY);
					uniquecoderecordEntity.setTotray(dto.getTotray());
					uniquecoderecordEntity.setTobox(dto.getTobox());
					uniquecoderecordEntity.setSku(dto.getSku());
					uniquecoderecordEntity.setLot(dto.getLot());
					uniquecoderecordEntity.setSourcetype(TradeConstant.TRADETYPE_RPA);
					uniquecoderecordEntity.setSourcetable("recipientsback");
					uniquecoderecordEntity.setSourceid(dto.getId());
					uniquecoderecordEntity.setSourcekey(null);
					uniquecoderecordEntity.setSourceline(null);
					uniquecoderecordEntity.setSourcelinelist(null);
					uniquecoderecordDao.insert(uniquecoderecordEntity);
				}
			}
		}
		// 设置领用状态

		if (hasBackQty.compareTo(BigDecimal.ZERO) > 0 || backQty.compareTo(BigDecimal.ZERO) > 0) {
			UpdateWrapper<RecipientsEntity> UpdateRecipients = new UpdateWrapper<RecipientsEntity>();
			UpdateRecipients.eq("recipientkey", dto.getRecipientkey());
			RecipientsEntity recipientsEntityUpdate = new RecipientsEntity();
			if (qty.compareTo(hasBackQty.add(backQty)) > 0) {
				recipientsEntityUpdate.setStatus(RecipientConstant.RECIPIENT_STATUS_15);
			} else {
				recipientsEntityUpdate.setStatus(RecipientConstant.RECIPIENT_STATUS_18);
			}
			recipientsDao.update(recipientsEntityUpdate, UpdateRecipients);

		}

		// 增加库存记录
		// 已存在记录:增加库存
		// 不存在记录:新增记录
		if (!lot10.equals("1")) {
			QueryWrapper<InventoryEntity> inventoryWrapper = new QueryWrapper<>();
			inventoryWrapper.eq("sku", dto.getSku());
			inventoryWrapper.eq("lot", dto.getLot());
			inventoryWrapper.eq("location", dto.getTolocation());
			inventoryWrapper.eq("tray", toTray);
			inventoryWrapper.eq("box", toBox);
			inventoryWrapper.orderByDesc("updatetime");
			List<InventoryEntity> inventoryEntityList = inventoryDao.selectList(inventoryWrapper);
			if (inventoryEntityList.size() > 0) {
				// 有多条记录,只对第一条进行更新
				InventoryEntity updateInventoryEntity = new InventoryEntity();
				updateInventoryEntity = inventoryEntityList.get(0);
				updateInventoryEntity.setQty(updateInventoryEntity.getQty().add(dto.getBackqty()));

				/*
				 * if (toloc.equalsIgnoreCase("PA")) {
				 * updateInventoryEntity.setPutqty(updateInventoryEntity.getPutqty().add(dto.
				 * getActqty())); }
				 */
				inventoryDao.updateById(updateInventoryEntity);
			} else {
				InventoryEntity addInventoryEntity = new InventoryEntity();
				addInventoryEntity.setSku(dto.getSku());
				addInventoryEntity.setLot(dto.getLot());
				addInventoryEntity.setLocation(dto.getTolocation());
				addInventoryEntity.setTray(dto.getTotray());
				addInventoryEntity.setBox(dto.getTobox());
				addInventoryEntity.setQty(dto.getBackqty());
				addInventoryEntity.setPickqty(new BigDecimal(0));
				addInventoryEntity.setPutqty(new BigDecimal(0));
				addInventoryEntity.setStatus(0);
				addInventoryEntity.setRemark(StringUtils.EMPTY);

				// 正常状态为0, 如果库位的状态是 HOLD ,库存设置为 1
				if (locationEntity.getLocStatus().equalsIgnoreCase(LocationConstant.LOC_STATUS_OK)) {
					addInventoryEntity.setStatus(InventoryConstant.INVENTORY_STATUS_OK);
				} else {
					addInventoryEntity.setStatus(InventoryConstant.INVENTORY_STATUS_HOLD);
				}
				inventoryDao.insert(addInventoryEntity);
			}

			// 增加交易记录
			TradeEntity addTradeEntity = new TradeEntity();
			String tradekey = sequenceService.getSequence("trade");
			addTradeEntity.setTradekey(tradekey);
			addTradeEntity.setTradetype(TradeConstant.TRADETYPE_RPA);
			addTradeEntity.setSku(dto.getSku());
			addTradeEntity.setQty(dto.getBackqty());
			addTradeEntity.setSourcetype("");
			addTradeEntity.setSourcekey(dto.getRecipientkey().toString());
			addTradeEntity.setSourceline(null);
			addTradeEntity.setSourcelinelist(null);
			addTradeEntity.setFromlocation("");
			addTradeEntity.setTolocation(dto.getTolocation());
			addTradeEntity.setFromtray("");
			addTradeEntity.setTotray(dto.getTotray());
			addTradeEntity.setFrombox("");
			addTradeEntity.setTobox(dto.getTobox());
			addTradeEntity.setFromlot1(null);
			addTradeEntity.setFromlot2(null);
			addTradeEntity.setFromlot3("");
			addTradeEntity.setFromlot4("");
			addTradeEntity.setFromlot5("");
			addTradeEntity.setFromlot6("");
			addTradeEntity.setFromlot7("");
			addTradeEntity.setFromlot8("");
			addTradeEntity.setFromlot9("");
			addTradeEntity.setFromlot10("");
			addTradeEntity.setFromlot11("");
			addTradeEntity.setFromlot12("");
			addTradeEntity.setFromlot13("");
			addTradeEntity.setFromlot14("");
			addTradeEntity.setFromlot15("");
			addTradeEntity.setTolot1(dto.getLot1());
			addTradeEntity.setTolot2(dto.getLot2());
			addTradeEntity.setTolot3(dto.getLot3());
			addTradeEntity.setTolot4(dto.getLot4());
			addTradeEntity.setTolot5(dto.getLot5());
			addTradeEntity.setTolot6(dto.getLot6());
			addTradeEntity.setTolot7(dto.getLot7());
			addTradeEntity.setTolot8(dto.getLot8());
			addTradeEntity.setTolot9(dto.getLot9());
			addTradeEntity.setTolot10(dto.getLot10());
			addTradeEntity.setTolot11(dto.getLot11());
			addTradeEntity.setTolot12(dto.getLot12());
			addTradeEntity.setTolot13(dto.getLot13());
			addTradeEntity.setTolot14(dto.getLot14());
			addTradeEntity.setTolot15(dto.getLot15());
			addTradeEntity.setRemark(StringUtils.EMPTY);
			addTradeEntity.setFromlot(null);
			addTradeEntity.setTolot(dto.getLot());
			tradeDao.insert(addTradeEntity);
		}
		return null;
	}

	@Override
	public List<RecipientsbackEntity> DC (Map<String, Object> params){

		List<RecipientsbackEntity> re = baseDao.selectListBySql1(getWrapper(params));
		return re;
	}

}